cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
cmake_policy(SET CMP0054 NEW)

set(PROJECT_VERSION 0.2.0)
project(SV2SaverDemo VERSION ${PROJECT_VERSION} LANGUAGES CXX)

add_definitions(-DAPI_EXPORTS)
set(CMAKE_BUILD_TYPE "Release")


## JETSON, X86_CUDA, X86_INTEL, OPENCV
message(STATUS "System:${CMAKE_HOST_SYSTEM_PROCESSOR}")
if(NOT DEFINED PLATFORM)
  message(FATAL_ERROR "PLATFORM NOT SPECIFIED!")
else()
  message(STATUS "PLATFORM: ${PLATFORM}")
  if(PLATFORM STREQUAL "JETSON")
    add_definitions(-DPLATFORM_JETSON)
    option(USE_GSTREAMER "BUILD WITH GSTREAMER." ON)
  elseif(PLATFORM STREQUAL "X86_CUDA")
    add_definitions(-DPLATFORM_X86_CUDA)
    option(USE_CUDA "BUILD WITH CUDA." ON)
    option(USE_FFMPEG "BUILD WITH FFMPEG." ON)
  elseif(PLATFORM STREQUAL "X86_INTEL")
    add_definitions(-DPLATFORM_X86_INTEL)
    option(USE_GSTREAMER "BUILD WITH GSTREAMER." ON)
  elseif(PLATFORM STREQUAL "OPENCV")
    option(USE_GSTREAMER "BUILD WITH GSTREAMER." ON)
  else()
    message(FATAL_ERROR "UNSUPPORTED PLATFORM!")
  endif()
endif()


if(USE_GSTREAMER)
  add_definitions(-DWITH_GSTREAMER)
  message(STATUS "GSTREAMER: ON")
endif()

if(USE_FFMPEG)
  add_definitions(-DWITH_FFMPEG)
  find_package(fmt REQUIRED)
  set(FFMPEG_LIBS libavutil.so libavcodec.so libavformat.so libavdevice.so libavfilter.so libswscale.so)
  message(STATUS "WITH_FFMPEG: ON")
endif()

find_package(SpireMS REQUIRED)

include_directories(${SpireMS_INCLUDE_DIRS})
find_package(OpenCV 4 REQUIRED)
message(STATUS "OpenCV library status:")
message(STATUS "    version: ${OpenCV_VERSION}")
message(STATUS "    libraries: ${OpenCV_LIBS}")
message(STATUS "    include path: ${OpenCV_INCLUDE_DIRS}")


include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)


if(USE_GSTREAMER)
  include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src/gstreamer)
  if(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "aarch64")
    include_directories(
      "/usr/include/gstreamer-1.0"
      "/usr/local/include/gstreamer-1.0"
      "/usr/include/glib-2.0"
      "/usr/lib/aarch64-linux-gnu/glib-2.0/include"
    )
  elseif(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "x86_64")
    include_directories(
      "/usr/include/gstreamer-1.0"
      "/usr/local/include/gstreamer-1.0"
      "/usr/include/glib-2.0"
      "/usr/lib/x86_64-linux-gnu/glib-2.0/include"
    )
  endif()
endif()

if(USE_FFMPEG)
  include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src/ffmpeg)
endif()


# Public header
set(
  public_HEADS
  include/sv2_video_base.h
  include/sv2_video_saver.h
  include/sv2_saver.h

)

set(
  SRCS
  src/sv2_video_base.cpp
  src/sv2_video_saver.cpp
  src/sv2_saver.cpp
)

if(USE_FFMPEG)
  if(USE_INTEL)
    file(GLOB ALG_SRC_FILES ${CMAKE_CURRENT_SOURCE_DIR}/src/ffmpeg/x86_intel/*.cpp)
    list(APPEND SRCS ${ALG_SRC_FILES})
  elseif(USE_CUDA)
    file(GLOB ALG_SRC_FILES ${CMAKE_CURRENT_SOURCE_DIR}/src/ffmpeg/x86_cuda/*.cpp)
    list(APPEND SRCS ${ALG_SRC_FILES})
  endif()
  file(GLOB ALG_SRC_FILES ${CMAKE_CURRENT_SOURCE_DIR}/src/ffmpeg/*.cpp)
  list(APPEND SRCS ${ALG_SRC_FILES})
endif()

if(USE_GSTREAMER)
  file(GLOB ALG_SRC_FILES ${CMAKE_CURRENT_SOURCE_DIR}/src/gstreamer/*.cpp)
  list(APPEND SRCS ${ALG_SRC_FILES})
endif()


add_executable(SV2SaverDemo SV2SaverDemo.cpp ${SRCS})
target_link_libraries(SV2SaverDemo ${OpenCV_LIBS} ${SpireMS_LIBS})
if(USE_FFMPEG)
  target_link_libraries(SV2SaverDemo ${FFMPEG_LIBS} fmt)
endif()



install(TARGETS SV2SaverDemo
  RUNTIME DESTINATION bin
)


